package com.promfy.anonymousshare;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.ToggleButton;

import com.promfy.anonymousshare.domain.Login;
import com.promfy.anonymousshare.service.LoginManager;

public class LoginActivity extends Activity implements OnClickListener{

	private EditText userNameEt;
	private EditText passwordEt;
	private Button loginBtn;
	private Button registerBtn;
	private RadioButton rememberPasswordRbtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		initViews();
		initData();
	}

	private void initData() {
		LoginManager lm = new LoginManager(this);
		Login login = lm.getLogin();
		if(login == null) return;
		if(!login.isRememberPassword()){
			userNameEt.setText(login.getUserName());
		}
	}

	private void initViews() {
		userNameEt = (EditText)findViewById(R.id.user_name_et);
		passwordEt = (EditText)findViewById(R.id.password_et);
		loginBtn = (Button)findViewById(R.id.login);
		registerBtn = (Button)findViewById(R.id.register);
		rememberPasswordRbtn = (RadioButton)findViewById(R.id.remember_password);
		
		loginBtn.setOnClickListener(this);
		registerBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login:
			LoginManager lm = new LoginManager(this);
			lm.login(userNameEt.getText().toString().trim(), passwordEt.getText().toString().trim(), rememberPasswordRbtn.isChecked());
			Intent intent = new Intent(LoginActivity.this, MainTabActivity.class);
			try{
				startActivity(intent);
			}catch (Exception e) {
				e.printStackTrace();
			}
			finish();
			break;
		case R.id.register:
			Intent intentRegister = new Intent(LoginActivity.this, RegisterActivity.class);
			try{
				startActivity(intentRegister);
			}catch (Exception e) {
				e.printStackTrace();
			}
			finish();
			break;

		default:
			break;
		}
		
	}

	

}
